package com.FindImportKeys.Utils;

import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.List;

import static java.awt.Desktop.*;

/**
 * @author cc
 * @date 2021/8/5 10:35
 * @Description
 */

public class Utis {
   private static Map<String,Boolean> isexistContent = new HashMap<String, Boolean>();

    public static File[] findFile(String filestring){
//        String path = "D:/Desktop/test";
        String path = filestring;
        File file = new File(path);
        File[] files = file.listFiles();
        return files;
    }

   public static void CopyDocOrTextToNewPath(String fileString,String newfile,String content) throws Exception {
       File[] files = findFile(fileString);
       List<String> docStrings = null;
       String[] contents = getUserinput(content);
       for (File file : files) {
           if (file.getName().endsWith(".doc")|file.getName().endsWith(".docx")){
               ReadDocAsStringContentAndWrrierTonewFile(contents, isexistContent, file,newfile);
//               ReadDocContentAndWrrierTonewFile(contents, isexistContent, file,newfile);
           }else {
               ReadText(file);
           }

       }
   }

    private static void ReadDocAsStringContentAndWrrierTonewFile(String[] contents, Map<String, Boolean> isexistContent, File file, String newfile) throws Exception {
        String temp =null;
        temp = ReadDocAndDocxUtils.readsimpleWord(file.getPath());
        for (int i=0;i<contents.length;i++){
            if (temp.contains(contents[i])){
                isexistContent.put(contents[i],true);
            }else {
                isexistContent.put(contents[i],false);
            }
        }
        Set<Map.Entry<String, Boolean>> entries = isexistContent.entrySet();
        for (Map.Entry<String, Boolean> entry : entries) {

            if (!entry.getValue()){
                System.out.println("没有找到合适的项目");
                return;
            }
        }
        CopyToNewFile(file,newfile);
    }

    private static void ReadDocContentAndWrrierTonewFile(String[] content, Map<String, Boolean> isexistContent, File file,String newfile) throws Exception {
        List<String> docStrings;
        docStrings = ReadDocAndDocxUtils.readWord(file.getPath());
        System.out.println("这是："+file.getName()+"文件的内容:"+ docStrings);
        for (String docString : docStrings) {
            //根据关键词筛选；
            for (int i=0;i<content.length;i++){
                if (docString.contains(content[i])){
                    isexistContent.put(content[i],true);
                }else {
                    isexistContent.put(content[i],false);
                }
            }
            Set<Map.Entry<String, Boolean>> entries = isexistContent.entrySet();
            for (Map.Entry<String, Boolean> entry : entries) {

                if (!entry.getValue()){
                    System.out.println("没有找到合适的项目");
                    return;
                }
                 CopyToNewFile(file,newfile);
            }

        }
    }

    private static void CopyToNewFile(File file,String newfile) throws IOException {
        //获取当前文件路径，并写入到流
        BufferedInputStream Getdoc = new BufferedInputStream(new FileInputStream(file),1024);
        //获取当前文件的名字
        String filename = file.getName();
        //创建新文件夹，把文件copy过去
        File newFile = new File(newfile+"\\\\"+filename);
        BufferedOutputStream setDoc = new BufferedOutputStream(new FileOutputStream(newFile),1024);
        int len =0;
        byte[] bytes = new byte[1024];
        while ((len=Getdoc.read(bytes))!=-1){
            setDoc.write(bytes,0,len);
        }
        setDoc.flush();
        setDoc.close();
    }

    private static String[] getUserinput(String youneddtype ) {
//        String youneddtype = null;
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入查询的类容：(','隔开)");
//        youneddtype =scanner.next();
        String[] content = youneddtype.split(",");

        return content;
    }

    private static void ReadText(File file) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
        String testfile = bufferedReader.readLine();
        System.out.println("这是："+file.getName()+"文件的内容:"+ testfile);
    }

//    public static void openfile() throws IOException {
//        File[] file = findFile();
//        Desktop desktop = getDesktop();
//        for (File file1 : file) {
//            desktop.open(file1);
//        }
//    }
    private static  void WriteDoc(){
    }
}
